//导入位置服务模块
import geoLocationManager from '@ohos.geoLocationManager';
import BusinessError from "@ohos.base";
//导入自定义的权限检查模块
import Check_Access from 'ets/service/Detector'

import { site } from '@kit.MapKit';

//定义结点的标签
const TAG_NODE0 = '------[Geo-Node0] '
const TAG_NODE1 = '------[Geo-Node1] '
const TAG_NODE2 = '------[Geo-Node2] '

/*
 *结点函数1:获取用户设备当前所处位置的经度和纬度数据
 */
async function Node1_Get_Geographical_Position(){

  //预定义返回值
  let output = {
    'position_x':null,
    'position_y':null,
    'position_z':null,
    'cityName':' 未知',
    'speed':null,
    //结点函数的执行状态，默认为失败
    'isFinished':false,
    'error':'无',
    'location':null
  }

  //检查定位功能是否可用
  if(!geoLocationManager.isLocationEnabled()){
    console.info(TAG_NODE1+'Location module loads fail')
    output.error = '定位功能不可用, 请检查设备或服务器'
    return
  }

  //定义需要输入的请求参数
  let requestInfo = {'priority': 0x203, 'scenario': 0x300,'maxAccuracy': 0}

  //等待模块完成获取地理位置的异步操作
  await geoLocationManager.getCurrentLocation(requestInfo).then((result) => {

    console.info(TAG_NODE1+'Succeed! Current location => latitude: ' + result.latitude+'; longitude: '+result.longitude+';');

    //记录获取的地理信息
    output.position_x = result.latitude
    output.position_y = result.longitude
    output.position_z = result.altitude
    output.speed = result.speed
    //结点函数的执行状态修改为成功
    output.isFinished = true

  }).catch((error) => {
    console.error(TAG_NODE1+'Get current location failed, error: ' + JSON.stringify(error));
    output.error = '地理位置获取失败'
  });

  return output

}



/*
 *结点函数2:获取用户设备当前所处的城市名称（中文）
 */
async function Node2_Get_City_Name(input){

  //预定义返回值
  let output = {
    'cityName': ' 未知',
    'position_x':input.position_x,
    'position_y':input.position_y,
    'position_z':input.position_z,
    'speed':input.speed,
    //结点函数的执行状态，默认为失败
    'isFinished': false,
    'error':'无',

  }

  let requestInfo: geoLocationManager.CurrentLocationRequest = {'priority': geoLocationManager.LocationRequestPriority.FIRST_FIX, 'timeoutMs': 10000};
  let locationChange = (err:BusinessError.BusinessError, location:geoLocationManager.Location):void => {
    if (err) {
      console.error('getCurrentLocation: err=' + JSON.stringify(err));
    }
    if (location) {
      console.log('getCurrentLocation: location=' + JSON.stringify(location));
    }
  };
  try {
    geoLocationManager.getCurrentLocation(requestInfo, locationChange);
  } catch (err) {
    console.error("errCode:" + (err as BusinessError.BusinessError).code + ",errMessage:" + (err as BusinessError.BusinessError).message);
  }

  return output

}

//导出可供调用的接口
export async function Get_Geo_Data(){

  console.info("Geo Run")
  //模块结点0
  if(!Check_Access()){


    console.error(TAG_NODE0+'Insufficient required permissions')
    return {
      'position_x':null,
      'position_y':null,
      'position_z':null,
      'cityName':' 未知',
      'speed':null,
      'isFinished':false,
      'error':'设备未获取相关权限',
      'site':null,
    }
  }

  //模块结点1
  let output = await Node1_Get_Geographical_Position()
  if (!output.isFinished) return output


  //模块结点2
  return await Node2_Get_City_Name(output)

}
